﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;

using TestingLibrary;

public partial class Teacher_CreateTest : System.Web.UI.Page
{
    List<AssessmentActions.MultipleChoiceQuestion> _mcQuestions;
    List<AssessmentActions.TrueFalseQuestion> _tfQuestions;
    List<String> _subjects = new List<string>();

    List<AssessmentActions.Question> _questions;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (tfList.Items.Count > 0)
        {
            ViewState["tf_index"] = tfList.SelectedIndex;
        }

        if (mcList.Items.Count > 0)
        {
            ViewState["mc_index"] = mcList.SelectedIndex;
        }

        if (subjects.Items.Count > 0)
        {
            ViewState["current_subject"] = subjects.SelectedItem.Text;
        }

        _questions = (List<AssessmentActions.Question>)ViewState["questions"];
        
        //string strConnString =
        //    System.Configuration.ConfigurationManager.ConnectionStrings
        //        ["dbConnectionString"].ConnectionString;

        //AssessmentActions.AssessmentDatabaseActions ada =
        //    new AssessmentActions.AssessmentDatabaseActions(strConnString);

        TestingWebService.TestServiceClient test_service_client =
            new TestingWebService.TestServiceClient();

        try
        {
            // get the mc questions and deseralize
            string[] serialize_mc_list = test_service_client.getAllMCQuestions();
            _mcQuestions = TestingFunctions.deserializeMCList(serialize_mc_list);

            // get the tf questions and deseralize
            string[] serialized_tf_list = test_service_client.getAllTFQuestions();
            _tfQuestions = TestingFunctions.deserializeTFList(serialized_tf_list);

            // get the subjects
            _subjects = new List<string>(test_service_client.getAllSubjects());

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // initialize the drop down boxes
        _setMCQuestions();
        _setTFQuestions();
        _setSubjects();
    }

    /// <summary>
    /// Query the database for the list of test subjects
    /// </summary>
    private void _setSubjects()
    {
        subjects.Items.Clear();

        foreach (string subject in _subjects)
        {
            ListItem item = new ListItem(subject);
            subjects.Items.Add(item);
        }
    }

    /// <summary>
    /// Initialize the Multiple Choice dropdown box
    /// </summary>
    private void _setMCQuestions()
    {
        mcList.Items.Clear();

        foreach (AssessmentActions.MultipleChoiceQuestion question in _mcQuestions)
        {
            String question_text =
                question.Text + ": ";
            foreach (String answer in question.Answers)
            {
                question_text += answer + " | ";
            }
            question_text += "Correct: " + question.Answers[question.Correctanswer];
            ListItem item = new ListItem(question_text);
            mcList.Items.Add(item);
        }
    }

    /// <summary>
    /// Initialize the True/False dropdown box
    /// </summary>
    private void _setTFQuestions()
    {
        tfList.Items.Clear();

        foreach (AssessmentActions.TrueFalseQuestion question in _tfQuestions)
        {
            String question_text =
                question.Text + ": " +
                question.getCorrectAnswer().ToString();

            ListItem item = new ListItem(question_text);
            tfList.Items.Add(item);
        }
    }

    /// <summary>
    /// Add the currently selected MC question to the bank
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void addMCButton_Click(object sender, EventArgs e)
    {
        int test_summary_index = testSummary.Items.Count;

        int mc_index;
        try
        {
            mc_index = (int)ViewState["mc_index"];
        }
        catch (Exception)
        {
            return;
        }
        if (_questions == null)
        {
            _questions = new List<AssessmentActions.Question>();
        }
        _questions.Add(_mcQuestions[mc_index]);
        testSummary.Items.Add(mcList.Items[mc_index]);

        ViewState["questions"] = _questions;
    }

    /// <summary>
    /// Add the currently selected TF question to the bank
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void addTFButton_Click(object sender, EventArgs e)
    {
        int test_summary_index = testSummary.Items.Count;

        int tf_index;
        try
        {
            tf_index = (int)ViewState["tf_index"];
        }
        catch (Exception)
        {
            return;
        }

        if (_questions == null)
        {
            _questions = new List<AssessmentActions.Question>();
        }

        _questions.Add(_tfQuestions[tf_index]);
        testSummary.Items.Add(tfList.Items[tf_index]);

        ViewState["questions"] = _questions;
    }

    /// <summary>
    /// Create the assessment from the provided fields
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void createButton_Click(object sender, EventArgs e)
    {
        try
        {
            string subject = (string)ViewState["current_subject"];
            AssessmentActions.Assessment assessment =
                new AssessmentActions.Assessment();
            assessment.Name = testTitle.Text;
            assessment.CreatedDate = DateTime.Now;

            foreach (AssessmentActions.Question question in _questions)
            {
                assessment.addQuestion(question);
            }

            TestingWebService.TestServiceClient test_service_client =
            new TestingWebService.TestServiceClient();

            string serialized_assessment = TestingFunctions.serializeAssessment(assessment);

            test_service_client.addAssessment(subject, serialized_assessment);

            // Connect to the database
            //string strConnString =
            //System.Configuration.ConfigurationManager.ConnectionStrings
            //    ["dbConnectionString"].ConnectionString;

            //// JSC TODO: Use web service instead
            //AssessmentActions.AssessmentDatabaseActions ada =
            //new AssessmentActions.AssessmentDatabaseActions(strConnString);

            //// create the new assessment
            //ada.addAssessmentToDatabase(subject, assessment);

        }
        catch (Exception)
        {
            return;
        }
    }

    /// <summary>
    /// Reset the current test
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void resetButton_Click(object sender, EventArgs e)
    {
        testSummary.Items.Clear();
        _questions.Clear();
    }
}